<template>
  <div class="account-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="account-dialog"
      draggable
    >
      <el-form ref="accountRef" :model="state.form" :rules="state.rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="accountname">
              <el-input v-model="state.form.accountname" placeholder="用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="accountgsm">
              <el-input v-model="state.form.accountgsm" placeholder="公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源" prop="accountssqd">
              <el-input v-model="state.form.accountssqd" placeholder="来源" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推荐人id" prop="accounttjsid">
              <el-input v-model="state.form.accounttjsid" placeholder="推荐人id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推荐人code" prop="accounttjscode">
              <el-input v-model="state.form.accounttjscode" placeholder="推荐人code" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址" prop="accountgsdz">
              <el-input v-model="state.form.accountgsdz" placeholder="公司地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纳税人识别号" prop="accountlsrsbh">
              <el-input v-model="state.form.accountlsrsbh" placeholder="纳税人识别号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业类型" prop="accountqylx">
              <el-input v-model="state.form.accountqylx" placeholder="企业类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属行业" prop="accountsshy">
              <el-input v-model="state.form.accountsshy" placeholder="所属行业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="accountlxr">
              <el-input v-model="state.form.accountlxr" placeholder="联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号" prop="accountlxrphone">
              <el-input v-model="state.form.accountlxrphone" placeholder="联系人手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="AI客服名称" prop="accountainame">
              <el-input v-model="state.form.accountainame" placeholder="AI客服名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计费类型" prop="accountjflx">
              <el-input v-model="state.form.accountjflx" placeholder="计费类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文本可用次数" prop="accounttxtnum">
              <el-input v-model="state.form.accounttxtnum" placeholder="文本可用次数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片可用次数" prop="accounttxtimgnum">
              <el-input v-model="state.form.accounttxtimgnum" placeholder="图片可用次数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色组" prop="accountgrouplist">
              <el-input v-model="state.form.accountgrouplist" placeholder="角色组" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单一权限" prop="accountpopedom">
              <el-input v-model="state.form.accountpopedom" placeholder="单一权限" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="账号创建人" prop="accountcreateuser">
              <el-input v-model="state.form.accountcreateuser" placeholder="账号创建人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号开通时间" prop="accountcreatedate">
              <el-input v-model="state.form.accountcreatedate" placeholder="账号开通时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号到期时间" prop="accountenddate">
              <el-input v-model="state.form.accountenddate" placeholder="账号到期时间" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="业务介绍" prop="accountywjs">
              <el-input v-model="state.form.accountywjs" placeholder="业务介绍" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号是否可用" prop="accountstatus">
              <el-input v-model="state.form.accountstatus" placeholder="账号是否可用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请选择实例" prop="">
              <el-select v-model="state.form.accountalicccinstanceid" placeholder="选择绑定的实例" class="w-full">
                <el-option v-for="(item, index) in state.example" :key="index" :label="item.Id" :value="item.Id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="tips-box" v-if="!Object.keys(state.editView).length">
        <p>
          <el-icon>
            <InfoFilled />
          </el-icon>
          <span>新建账号成功后，默认密码：123456</span>
        </p>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {aliccc_allin} from '@/api/base'

import useAccountManageStore from '@store/modules/accountManage'
import {filterObject} from '@/utils/index'
import md5 from 'js-md5'
import {nextTick} from 'vue'

const accountManageStore = useAccountManageStore()
const {proxy} = getCurrentInstance()
const accountRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    accountname: '',
    accountgsm: '',
    accountssqd: '',
    accounttjsid: '',
    accounttjscode: '',
    accountgsdz: '',
    accountlsrsbh: '',
    accountqylx: '',
    accountsshy: '',
    accountlxr: '',
    accountlxrphone: '',
    accountainame: '',
    accountjflx: '',
    accounttxtnum: '',
    accounttxtimgnum: '',
    accountgrouplist: '',
    accountpopedom: '',
    // accountcreateuser: '',
    // accountcreatedate: '',
    // accountenddate: '',
    accountywjs: '',
    accountstatus: '',
    accountalicccinstanceid: ''
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
    accountstatus: [{required: true, message: '账号是否可用不能为空', trigger: 'blur'}]
  },
  example: []
})

const emits = defineEmits(['refresh'])

const getDataView = (accountid) => {
  nextTick(async () => {
    const result = await accountManageStore.view({accountid: accountid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  accountRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.accountid = state.editView.accountid
        await accountManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        state.form.accountpwd = md5('123456')
        await accountManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      accountRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

/** 实例列表 */
const getExample = async () => {
  const response = await aliccc_allin({PageNumber: 1, PageSize: 99, pagevalue: 221})
  state.example = response?.Data?.List
}

const cancelDialog = () => {
  state.editView = {}
  accountRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  await getExample()
  if (Object.keys(data).length) {
    await getDataView(data.accountid)
  }
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.accountname}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
